//添加多个参数注意使用[]
layui.use(['form', 'jquery'], function () {

    const form = layui.form,
        $ = layui.jquery;

    const id = sessionStorage.getItem("signRecordId");

    createSignStatus('signStatus');
    findById(id);

    /**
     * 查询单个
     * @param id
     */
    function findById(id) {

        let result = commonAjax('/sign/findById', {id: id}, 'get');
        setData(result.data);
    }

    /**
     * 赋值
     */
    function setData(data) {
        $('#id').val(data.id);
        $('#username').val(data.username);
        $('input[name=signStatus][value=' + data.signStatus + ']').click();
        $('#lateTime').val(data.lateTime);
        //刷新单选框样式
        form.render();
    }

    /**
     * 绑定点击事件
     */
    $('.layui-form-radio').bind('click', function () {
        let signStatus = $("input[name='signStatus']:checked").val();
        let lateTime = $('#lateTime');
        $('#updateTime').val(getNowFormatDate());//修改时间
        if (signStatus === 'late') {
            lateTime.attr('class', "show");
        } else {
            lateTime.attr('class', "hidden");
        }
    });

    /**
     * 保存
     */
    form.on('submit(commit)', function () {
        let data = {
            id: $('#id').val(),
            signStatus: $("input[name='signStatus']:checked").val(),
            updateTime: $('#updateTime').val(),
            lateTime: $('#lateTime').val()
        };

        let result = commonAjax('/sign/update', JSON.stringify(data), 'post');
        if (result.code === 200) {
            layerMsgSaveSuccess();
        } else {
            layerMsgSaveFail();
        }

        return false;
    });

    /**
     * 签到状态
     */
    function createSignStatus(type) {
        let data = {
            type: type
        };
        let result = commonAjax("/code/findByType", data, "get");
        let html = '';
        for (let i = 0; i < result.data.length; i++) {
            html += '<input type="radio" class="signStatus" name="signStatus"' +
                ' value="' + result.data[i].code + '" title="' + result.data[i].value + '">'
        }
        $('#signStuStatus').html(html);
    }

});